package com.seabook.store.goodmgr.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.seabook.store.auth.web.UserSession;
import com.seabook.store.goodmgr.domain.Category;
import com.seabook.store.goodmgr.service.ProductService;
import com.seabook.store.util.ServiceLocator;

@Controller
@SessionAttributes("categories")
public class CategoryController {
    private final ProductService productService;

    static List<Category> categories = null;

    public CategoryController() {
        this.productService = (ProductService) ServiceLocator
                .getBean("productService");
        categories = productService.getSubCategories(0);
    }

    private void refreshCategory() {
        categories = productService.getSubCategories(0);
    }

    @RequestMapping(value = "/admin/category")
    public String getCategories(HttpServletRequest request) {
        request.setAttribute("categories", categories);
        return "category";
    }

    @RequestMapping(value = "/admin/savecategory")
    public String saveCategory(@RequestParam("category.id") int id,
            @RequestParam("category.order") int order,
            @RequestParam("category.name") String name,
            @RequestParam("category.categoryId") int parentId,
            HttpServletRequest request) {
        Category category = new Category();
        category.setId(id);
        category.setName(name);
        category.setCategoryId(parentId);
        category.setOrder(order);
        productService.saveCategory(category);
        refreshCategory();
        request.setAttribute("categories", categories);
        return "category";
    }
    
    @RequestMapping(value = "/admin/deletecategory")
    public String deleteCategory(@RequestParam("category.id") int id,
            HttpServletRequest request) {
        boolean delete = productService.deleteCategory(id);
        if(delete){
            refreshCategory();
        }else{
            request.setAttribute("msg", "Please ensure that the category you want to delete does not have any sub category or product.");
        }
        request.setAttribute("categories", categories);
        return "category";
    }

    @RequestMapping(value = "/products", method = RequestMethod.GET)
    public String getCategories(@RequestParam("categoryId") int categoryId,
            HttpServletRequest request) {
        if (null == categories) {
            categories = productService.getSubCategories(0);
        }
        Category category = findCategory(categories, categoryId);
        request.getSession().setAttribute("categories", categories);
        request.setAttribute("curCategoryId", categoryId);
        request.setAttribute("productList", this.productService
                .getProductByCategory(categoryId));
        return "main";
    }

    private Category findCategory(Category category, int categoryId) {
        if (category.getId() == categoryId) {
            return category;
        } else {
            return findCategory(category.getChildrenCategories(), categoryId);
        }
    }

    private Category findCategory(List<Category> categories, int categoryId) {
        if (null == categories || 0 == categories.size())
            return null;
        for (Category c : categories) {
            Category cat = findCategory(c, categoryId);
            if (null != cat)
                return cat;
        }
        return null;
    }

}
